<?php

namespace api\modules\coupon\controllers;

use api\extensions\ApiBaseController;
use api\services\active\ActiveCouponService;

class CouponController extends ApiBaseController
{

    /**
     * 券领取列表
     * * */
    public function actionList()
    {
        $params = \Yii::$app->request->post();
        $rules = [
            [['code'], 'required', 'message' => '{attribute}属必填项'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        return ActiveCouponService::getCouponList($this->params['code'], true);
    }

    /**
     * 领券
     * * */
    public function actionReceive()
    {
        $params = \Yii::$app->request->post();
        $rules = [
            [['id'], 'required', 'message' => '{attribute}属必填项'],
            [['code', 'channel_code'],  'default', 'value' => 1, 'message' => '额外参数']
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        return ActiveCouponService::receive($this->params);
    }

    // 我的优惠券
    public function actionUserCouponList()
    {
        $params = \Yii::$app->request->post();
        $rules = [
            [['state'],  'default', 'value' => 1, 'message' => '额外参数']
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        return ActiveCouponService::userCouponList($this->params);
    }

    // 我的优惠券
    public function actionUserCouponDetail()
    {
        $params = \Yii::$app->request->post();
        $rules = [
            [['code'], 'required', 'message' => '{attribute}属必填项'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        return ActiveCouponService::userCouponDetail($this->params['code']);
    }


    /**
     * 会员可用券
     * **/
    public function actionCouponUsable()
    {
        $params = \Yii::$app->request->post();
        $rules = [
            [['mkey'], 'required', 'message' => '{attribute}属必填项'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        return ActiveCouponService::getCouponUsable($this->params['mkey']);
    }


    // 获取商品可适用券
    public function actionGoodsCoupon()
    {
        $params = \Yii::$app->request->post();
        $rules = [
            [['gid'], 'required', 'message' => '{attribute}属必填项'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        return ActiveCouponService::getGoodsCoupon($this->params['gid']);
    }
}
